import sys
sys.setrecursionlimit(10**8)
total_input=int(input())
for _ in range(total_input):
s=input()
dic={}
def do(ini,s):
idx,maxx,minn,sign=ini+1,0,0,True
while s[idx]!=')':
char=s[idx]
if char=='?':
if sign:maxx+=1
else:minn+=1
elif char=='+' :sign=True
elif char=='-' :sign=False
elif char=='(':
arr=do(idx,s)
if sign:
maxx+=arr[0]
minn+=arr[1]
else:
maxx+=arr[1]
minn+=arr[0]
idx=arr[2]
idx+=1
dic[ini]=maxx
return [maxx,minn,idx]
do(0,s)
ques=int(input())
ans=[]
for __ in range(ques):
ini,fin=map(int,input().split())
if ini==fin:ans.append(1)
else:ans.append(dic[ini-1])
print(*ans)
1475A - Odd Divisor | 1454B - Unique Bid Auction |
978C - Letters | 501B - Misha and Changing Handles |
1496A - Split it | 1666L - Labyrinth |
1294B - Collecting Packages | 1642B - Power Walking |
1424M - Ancient Language | 600C - Make Palindrome |
1669D - Colorful Stamp | 1669B - Triple |
1669A - Division | 1669H - Maximal AND |
1669E - 2-Letter Strings | 483A - Counterexample |
3C - Tic-tac-toe | 1669F - Eating Candies |
1323B - Count Subrectangles | 991C - Candies |
1463A - Dungeon | 1671D - Insert a Progression |
1671A - String Building | 1671B - Consecutive Points Segment |
1671C - Dolce Vita | 1669G - Fall Down |
4D - Mysterious Present | 1316B - String Modification |
1204A - BowWow and the Timetable | 508B - Anton and currency you all know |